import { Injectable, OnApplicationBootstrap } from '@nestjs/common';
import { AdminUserService } from './modules/admin-user/admin-user.service';
import { RoleService } from './modules/role/role.service';
// 初始值
@Injectable()
export class DefaultDataService implements OnApplicationBootstrap {
    constructor(
        private readonly adminUserRepository: AdminUserService,
        private readonly roleService: RoleService
    ) { }

    async onApplicationBootstrap() {
        await this.checkAndAddDefaultData();
    }
    async checkAndAddDefaultData() {
        /** 创建默认管理员 */
        const adminUser = await this.adminUserRepository.addUserIfNotExists();
        if (adminUser) {
            /** 创建默认角色 */
            const role = await this.roleService.addRoleIfNotExists(adminUser.id)
            /** 绑定角色 */
            role && await this.adminUserRepository.adminUserBindRole({ roleId: role.id, userId: adminUser.id })

        }
    }
}
